package wujinye.com.dao;

import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import wujinye.com.constant.Const;
import wujinye.com.dao.base.BaseDao;
import wujinye.com.model.Conference;
import wujinye.com.utils.JDBCUtil;
import wujinye.com.utils.StrUtil;

import java.sql.SQLException;
import java.util.List;

public class ConferenceDao extends BaseDao<Conference> {

    /**
     * 查找会议信息
     *
     * @param search
     * @param deptName
     * @param page
     * @return
     */
    public List<Conference> findMeetingList(String search, String deptName, int page) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        //开启下划线->驼峰转换所用
        RowProcessor processor = new BasicRowProcessor(new GenerousBeanProcessor());
        List<Conference> list = null;
        StringBuilder sql = new StringBuilder();
        sql.append("select * from conference ");
        try {
            boolean searchFlag = false;
            boolean deptNameFlag = false;
            if (StrUtil.checkString(search)) {
                sql.append("where title like ? ");
                search = "%" + search + "%";
                searchFlag = true;
            }
            if (StrUtil.checkString(deptName)) {
                deptNameFlag = true;
                if (searchFlag) {
                    sql.append("and dept_name = ? ");
                } else {
                    sql.append("where dept_name = ? ");
                }

            }
            sql.append("order by id desc limit ?,?;");
            if (searchFlag) {
                if (deptNameFlag) {
                    list = qr.query(sql.toString(), new BeanListHandler<>(Conference.class, processor), search, deptName, (page - 1) * Const.MAX_MEETING_PAGE_SIZE, Const.MAX_MEETING_PAGE_SIZE);
                } else {
                    list = qr.query(sql.toString(), new BeanListHandler<>(Conference.class, processor), search, (page - 1) * Const.MAX_MEETING_PAGE_SIZE, Const.MAX_MEETING_PAGE_SIZE);
                }
            } else {
                if (deptNameFlag) {
                    list = qr.query(sql.toString(), new BeanListHandler<>(Conference.class, processor), deptName, (page - 1) * Const.MAX_MEETING_PAGE_SIZE, Const.MAX_MEETING_PAGE_SIZE);
                } else {
                    list = qr.query(sql.toString(), new BeanListHandler<>(Conference.class, processor), (page - 1) * Const.MAX_MEETING_PAGE_SIZE, Const.MAX_MEETING_PAGE_SIZE);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 统计会议总数
     *
     * @param search
     * @param deptName
     * @return
     */
    public long getTotalMeetings(String search, String deptName) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        long total = 0L;
        StringBuilder sql = new StringBuilder();
        sql.append("select count(*) from conference ");
        try {
            boolean searchFlag = false;
            boolean deptNameFlag = false;
            if (StrUtil.checkString(search)) {
                sql.append("where title like ? ");
                search = "%" + search + "%";
                searchFlag = true;
            }
            if (StrUtil.checkString(deptName)) {
                deptNameFlag = true;
                if (searchFlag) {
                    sql.append("and dept_name = ? ");
                } else {
                    sql.append("where dept_name = ? ");
                }
            }
            if (searchFlag) {
                if (deptNameFlag) {
                    total = qr.query(sql.toString(), new ScalarHandler<Long>(), search, deptName);
                } else {
                    total = qr.query(sql.toString(), new ScalarHandler<Long>(), search);
                }
            } else {
                if (deptNameFlag) {
                    total = qr.query(sql.toString(), new ScalarHandler<Long>(), deptName);
                } else {
                    total = qr.query(sql.toString(), new ScalarHandler<Long>());
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return total;
    }

    /**
     * 设置会议状态，初始为0（未开始），1（正在进行），2（已结束），3（已取消）
     * @param status
     */
    public void setStatus(Integer status,Integer mId) {
        String sql = "update conference set status = ? where id = ?;";
        Object[] obj = {status,mId};
        adu(sql,obj);
    }

    /**
     * 根据会议Id查找对应的会议信息
     * @param mId
     * @return
     */
    public Conference findMeetingById(Integer mId) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        //开启下划线->驼峰转换所用
        RowProcessor processor = new BasicRowProcessor(new GenerousBeanProcessor());
        String sql = "select * from conference where id = ?;";
        Conference conference = null;
        try {
            conference = qr.query(sql,new BeanHandler<>(Conference.class,processor),mId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return conference;
    }

    /**
     * 根据会议ID，设置该会议的status=3
     * @param mId
     */
    public void cancelMeeting(Integer mId) {
        String sql = "update conference set status = 3 where id = ?;";
        Object[] obj = {mId};
        adu(sql,obj);
    }


    /**
     * 添加会议
     * @param conference
     */
    public Object addMeeting(Conference conference) {
        QueryRunner qr = new QueryRunner(JDBCUtil.getDataSource());
        String sql = "insert into conference values(?,?,?,?,?,?,?,?,?);";
        Object[] obj = {conference.getId(),conference.getDeptName(),conference.getDeptId(),conference.getTitle(),conference.getContent(),conference.getPublishDate(),conference.getStartTime(),conference.getStatus(),conference.getUserId()};
        adu(sql,obj);
        Object increaseId = null;
        try {
            increaseId=qr.query("select last_insert_id()", new ScalarHandler<>());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return increaseId;
    }
}
